package com.hotel.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotel.common.Result;
import com.hotel.service.PointsRecordService;
import com.hotel.vo.PointsRecordVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * 积分记录管理控制器
 * 
 * @author Hotel Management Team
 */
@RestController
@RequestMapping("/points-record")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class PointsRecordController {

    @Autowired
    private PointsRecordService pointsRecordService;

    /**
     * 分页查询积分记录
     */
    @GetMapping("/list")
    public Result<Page<PointsRecordVO>> getPointsRecordPage(
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "20") int size,
            @RequestParam(required = false) Long memberId,
            @RequestParam(required = false) String type) {
        Page<PointsRecordVO> page = pointsRecordService.getPointsRecordPage(current, size, memberId, type);
        return Result.success(page);
    }

    /**
     * 添加积分记录
     */
    @PostMapping
    public Result<Void> addPointsRecord(
            @RequestParam @NotNull Long memberId,
            @RequestParam @NotNull String type,
            @RequestParam @NotNull Integer points,
            @RequestParam(required = false) String source,
            @RequestParam(required = false) String orderNo,
            @RequestParam(required = false) String description) {
        boolean success = pointsRecordService.addPointsRecord(memberId, type, points, source, orderNo, description);
        if (success) {
            return Result.success("积分记录添加成功");
        } else {
            return Result.error("积分记录添加失败");
        }
    }

    /**
     * 获取会员积分记录
     */
    @GetMapping("/member/{memberId}")
    public Result<List<PointsRecordVO>> getMemberPointsRecords(
            @PathVariable @NotNull Long memberId,
            @RequestParam(defaultValue = "10") int limit) {
        List<PointsRecordVO> records = pointsRecordService.getMemberPointsRecords(memberId, limit);
        return Result.success(records);
    }

    /**
     * 获取积分统计
     */
    @GetMapping("/stats")
    public Result<Map<String, Object>> getPointsStats() {
        Map<String, Object> stats = pointsRecordService.getPointsStats();
        return Result.success(stats);
    }

    /**
     * 获取积分趋势
     */
    @GetMapping("/trend")
    public Result<List<Map<String, Object>>> getPointsTrend(@RequestParam(defaultValue = "30") Long days) {
        List<Map<String, Object>> trend = pointsRecordService.getPointsTrend(days);
        return Result.success(trend);
    }

    /**
     * 获取积分来源统计
     */
    @GetMapping("/source-stats")
    public Result<List<Map<String, Object>>> getPointsSourceStats() {
        List<Map<String, Object>> sourceStats = pointsRecordService.getPointsSourceStats();
        return Result.success(sourceStats);
    }

    /**
     * 获取积分使用统计
     */
    @GetMapping("/usage-stats")
    public Result<List<Map<String, Object>>> getPointsUsageStats() {
        List<Map<String, Object>> usageStats = pointsRecordService.getPointsUsageStats();
        return Result.success(usageStats);
    }

    /**
     * 获取会员积分排行
     */
    @GetMapping("/member-ranking")
    public Result<List<Map<String, Object>>> getMemberPointsRanking(@RequestParam(defaultValue = "10") int limit) {
        List<Map<String, Object>> ranking = pointsRecordService.getMemberPointsRanking(limit);
        return Result.success(ranking);
    }

    /**
     * 获取积分类型统计
     */
    @GetMapping("/type-stats")
    public Result<List<Map<String, Object>>> getPointsTypeStats() {
        List<Map<String, Object>> typeStats = pointsRecordService.getPointsTypeStats();
        return Result.success(typeStats);
    }

    /**
     * 获取月度积分统计
     */
    @GetMapping("/monthly-stats")
    public Result<List<Map<String, Object>>> getMonthlyPointsStats(@RequestParam(defaultValue = "12") int months) {
        List<Map<String, Object>> monthlyStats = pointsRecordService.getMonthlyPointsStats(months);
        return Result.success(monthlyStats);
    }

    /**
     * 获取积分活跃度统计
     */
    @GetMapping("/activity-stats")
    public Result<Map<String, Object>> getPointsActivityStats(@RequestParam(defaultValue = "30") int days) {
        Map<String, Object> activityStats = pointsRecordService.getPointsActivityStats(days);
        return Result.success(activityStats);
    }

    /**
     * 获取会员积分趋势
     */
    @GetMapping("/member/{memberId}/trend")
    public Result<List<Map<String, Object>>> getMemberPointsTrend(
            @PathVariable @NotNull Long memberId,
            @RequestParam(defaultValue = "30") int days) {
        List<Map<String, Object>> trend = pointsRecordService.getMemberPointsTrend(memberId, days);
        return Result.success(trend);
    }
}